//
//  StellEmumView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/22.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "StellEmumView.h"
#import "StellEmumTableViewCell.h"

@interface StellEmumView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>{
    NSArray * dataSource;
}
@end

@implementation StellEmumView

-(id)initPopupEmumview:(NSArray *)emumListArr theCGRect:(CGRect)rect{
    if (self = [super initWithFrame:kAppWindow.bounds inSuperView:kAppWindow]) {
        dataSource = emumListArr;
        CGFloat celljieHeight;
        if (iPhoneX) {
            celljieHeight = 88 + kHeightProportion_In_iPhoneTriploid(94);
        }else{
            celljieHeight = 64 + kHeightProportion_In_iPhoneTriploid(94);
        }
        GLGWView * emumBankgroundView = [[GLGWView alloc] initWithFrame:CGRectMake(KScreenWidth - kWidthProportion_In_iPhoneTriploid(173), rect.origin.y+ celljieHeight, kWidthProportion_In_iPhoneTriploid(158), 0) inSuperView:self];
        UITableView * emumTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, emumBankgroundView.width , 0) style:UITableViewStylePlain];
        emumTabelView.layer.cornerRadius = 6.0;
        emumTabelView.layer.masksToBounds = YES;
        emumTabelView.backgroundColor = [UIColor whiteColor];
        emumTabelView.delegate = self;
        emumTabelView.dataSource = self;
        emumTabelView.rowHeight = kHeightProportion_In_iPhoneTriploid(22);
        emumTabelView.showsVerticalScrollIndicator = NO;
        emumTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        emumTabelView.scrollEnabled = NO;
        [emumBankgroundView addSubview:emumTabelView];
        emumBankgroundView.backgroundColor = kRandomColor;
        [emumBankgroundView setViewShadowWidth:3.0 theCornerRadius:6.0];
        [UIView animateWithDuration:0.25 animations:^{
            emumBankgroundView.height = kHeightProportion_In_iPhoneTriploid(22)*emumListArr.count;
            emumTabelView.height = emumBankgroundView.height;
        }];
        //手势
        UITapGestureRecognizer * tap_hider = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap_hiderSelf:)];
        tap_hider.delegate = self;
        tap_hider.numberOfTouchesRequired = 1;
        tap_hider.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap_hider];
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]){
        return NO;
    }
    return  YES;
}
-(void)tap_hiderSelf:(UITapGestureRecognizer *)tap{
    [self removeFromSuperview];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    StellEmumTableViewCell * cell = [StellEmumTableViewCell cellWithTableView:tableView];
    cell.titleStr = [NSString stringWithFormat:@"%@",dataSource[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
    if (self.Block_SelectText) {
        self.Block_SelectText([NSString stringWithFormat:@"%@",dataSource[indexPath.row]], indexPath.row);
    }
    
}
@end

